// pages/media/media.js
let http = require('../../utils/api.js');
let domain = require('../../utils/api.js');
import {
    login,
    myHttps
} from '../../utils/util.js';
import {
    myHttpss
} from '../../utils/json.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mediaList:'',//选择后的列表
        contentList: [],
        empty: true,//已选择的媒体是否显示
        ischoose: true,//是否选中
        domain:'',//ip
        media:'',//传给后台选择得专业媒体
        medialist:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        var that = this
        wx.getStorage({
            key: 'mediaData',
            success: function (res) {
                that.setData({
                    index: options.index,
                    medialist: res.data
                });
            }
        })
        this.setData({
            domain: domain.domain
        })
        this.media()//专业媒体列表接口
    },

    //点击已选择
    cancel (e) {
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        // console.log(index)
        // console.log(this.data.contentList[index])
        this.data.contentList[index].isFav = false
        this.setData({
            contentList: this.data.contentList
        })
    },
    //点击选择
    add(e) {
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        // console.log(index)
        // console.log(this.data.contentList[index])
        this.data.contentList[index].isFav = true
        this.setData({
            contentList: this.data.contentList
        })  
    },
    //确定的点击事件
    btn() {
        var media = [];//带过去得专业媒体
        for (var i = 0; i < this.data.contentList.length; i++) {
            if (this.data.contentList[i].isFav == true) {
                var media_name = this.data.contentList[i].title
                var platform_id = this.data.contentList[i].id
                var media_id = this.data.contentList[i].media_id
                media.push({ 
                    media_name: media_name,
                    platform_id: platform_id,
                    media_id:media_id
                })
            }
        }
        this.setData({
            media: media,//要带到创建主题页面
        })
        // console.log(this.data.media)
        let that = this
        wx.setStorage({
            key: 'mediaData',
            data: this.data.media
        })
        if (this.data.index) {
            wx.redirectTo({
                url: '../EditorTheme/EditorTheme?index=' + 1,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
            })
        } else {
            wx.navigateBack({
                url: '../createTheme/createTheme',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
            })
        }

    },
    // 媒体列表
    media () {
        console.log(this.data.medialist)
        var that = this
        let url = http.api.media + 'PM'
        myHttpss({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].isFav = false
                   
                    if (this.data.medialist){
                        for (var j = 0; j < this.data.medialist.length; j++){
                            if (res.data.content[i].title == this.data.medialist[j].media_name){
                                res.data.content[i].isFav=true;
                            }
                        }
                    }else{
                        res.data.content[i].isFav = false
                    }
                    this.data.contentList.push(res.data.content[i])
                }
                this.setData({
                    contentList: this.data.contentList
                })
                // console.log(this.data.contentList)
            }
        }).catch(err => {})
    }
})